<template>
  <div class="Roles">
    <Breadcrumbs :breadtitle="{ '1': '权限管理', '2': '角色列表' }"></Breadcrumbs>
    <el-card>
      <!-- 添加角色 -->
      <el-row :gutter="20" type="flex" justify="end">
        <el-col :span="2" :pull="1">
          <el-button @click="dialogVisible=true"> 添加管理员</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="ShowRolesList[pagenum]" border style="width: 100%" stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['dbbottom','dbflex',index1==0?'dbtop':'']" v-for="(item1,index1) in scope.row.children" :key="item1.id">
              <!-- 一级 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightsId(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-arrow-right"></i>
              </el-col>
              <!-- 二级三级 -->
              <el-col :span="19">
                <el-row :class="['dbflex',index2 !==0?'dbtop':'']" v-for="(item2,index2) in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag closable type="warning" @close="removeRightsId(scope.row,item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-arrow-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag @close="removeRightsId(scope.row,item3.id)" closable :class="[index3 !==0?'dbtop':'']" type="success" v-for="(item3,index3) in item2.children" :key="item3.id">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column prop="address" label="操作" width="220">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="Putaddroles(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="Delroles(scope.row.id)">删除</el-button>
            <el-button size="mini" type="warning" @click="showSetRight(scope.row)">配置</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination :current-page="pagenum" :page-size="7" :total="total" layout="total, prev, pager, next, jumper" @current-change="handleCurrentChange">
      </el-pagination>

      <AddRoles @OutdialogVisible="Outdia" @getRoles="getRoles" :dialog="dialogVisible" :addrolesFather="addroles"></AddRoles>
      <SetRight @OutdiashowSetRight="diashowSetRight=false" @getRoles="getRoles" @EmptydefKeys="defKeys=[]" :roleId="roleId" :defKeys1="defKeys" :diashowSetRight="diashowSetRight"></SetRight>
    </el-card>
  </div>
</template>

<script>
import Breadcrumbs from '@/components/Breadcrumbs'
import AddRoles from '@/components/Roles/AddRoles'
import SetRight from '@/components/Roles/SetRight'
export default {
  name: 'Roles',
  data() {
    return {
      RolesList: null,
      ShowRolesList: {},
      dialogVisible: false,
      addroles: null,
      diashowSetRight: false,
      defKeys: [],   //需要传递到子的数据
      roleId: null,    //点击授权管理员的Id
      pagenum: 1, //当前的页数
      total: null
    }
  },
  components: {
    Breadcrumbs,
    AddRoles,
    SetRight
  },
  created() {
    this.getRoles()
  },
  methods: {
    getRoles() {
      this.$http.get('roles').then((res) => {
        if (res.data.meta.status !== 200) return
        console.log(res)
        this.RolesList = res.data.data
        this.total = res.data.data.length //总页数
         this.ShowRolesList={}
        for (var i = 0; i < Math.ceil(this.total / 7); i++) { //处理成每个7个数据
          let num = 7 * (i + 1)
          if (num > this.total) num = this.total
          this.ShowRolesList[i + 1] = this.RolesList.slice(7 * (i), num)
        }
      })

    },
    Outdia() {
      this.dialogVisible = false
      this.addroles = null
    },
    Putaddroles(item) {
      this.dialogVisible = true
      let addroles = {}
      addroles.roleName = item.roleName
      addroles.roleDesc = item.roleDesc
      addroles.id = item.id
      this.addroles = addroles
    },
    Delroles(id) {
      this.$confirm('此操作将永久删除该账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$http.delete(`roles/${id}`).then((res) => {
            if (res.data.meta.status !== 200) {
              this.$mess(res.data.meta.msg, 'error')
              return
            }
            this.$mess(res.data.meta.msg, 'success')
            this.getRoles()
          })
        })
        .catch(() => {
          this.$mess('已取消删除', 'info')
        })
    },
    removeRightsId(item, childrenId) {  //根据对应Id删除权限
      this.$confirm('此操作将永久删除该账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http.delete(`roles/${item.id}/rights/${childrenId}`).then(res => {
          if (res.data.meta.status !== 200) return
          this.$mess('删除权限成功', 'success')
          item.children = res.data.data
        })
      }).catch(() => {
        this.$mess('已取消删除', 'info')
      })
    },
    showSetRight(item) {  //展示分配权限的对话框
      this.diashowSetRight = true
      this.getLeafKeys(item, this.defKeys)
      this.roleId = item.id
    },
    getLeafKeys(item, arr) {  //递归形式，获取存在权限的Id
      if (!item.children) {  //如果 当前节点没有children
        return arr.push(item.id)
      }
      item.children.forEach(item1 => {
        this.getLeafKeys(item1, arr)
      });
    },
    handleCurrentChange(newNum) {   //监听 页码值 改变事件
      this.pagenum = newNum
      this.getRoles()
    },
  },
}
</script>

<style lang="less" scoped>
.Roles {
  .el-tag {
    margin: 7px;
  }
  .dbflex {
    display: flex;
    align-items: center;
  }
  .dbtop {
    border-top: 1px solid #eee;
  }
  .dbbottom {
    border-bottom: 1px solid #eee;
  }
}
</style>